package com.farmer.controller;

import com.farmer.pojo.Orders;
import com.farmer.service.UserService;
import com.farmer.utils.Result;

import com.farmer.pojo.User;
import com.farmer.utils.Upload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("add")
    public Result add(@RequestBody User user) {
        Result result =userService.add(user);
        return result;
    }

/*
* 封禁用户*/
    @RequestMapping("deleteUser")
    public Result deleteUser(@RequestParam("id") Integer id) {
        Result result = userService.deleteUser(id);
        return result;
    }
    /*解封用户*

     */
   @PostMapping("deblock")
   public Result deblock(@RequestParam("id") Integer id){
        Result result = userService.deblock(id);
        return result;
   }

    @PutMapping("update")
    public Result update(@RequestBody User user) {
        Result result = userService.update(user);
        return result;
    }

    @GetMapping("getById")
    public Result getById(@RequestParam Integer id) {
        Result result = userService.getById(id);
        return result;
    }

    @GetMapping("getAll")
    public Result getAll() {
        Result result = userService.getAll();
        return result;
    }

    /*
     * 根据标题模糊查询*/
    @GetMapping("getByTitleLike")
    public Result getByTitleLike(@RequestParam String name){
        Result result = userService.getByTitleLike(name);
        return result;
    }
/*
* 用户登录*/
    @PostMapping("login")
    public Result login(@RequestBody User user){
        Result result = userService.login(user);
        return  result;
    }

/*
* 获取用户个人信息*/
    @GetMapping("getUserInfo")
    public Result getUserInfo(@RequestHeader String token){
        Result result = userService.getUserInfo(token);
        return result;

        }
   /*
   * 检查用户姓名与数据库中的姓名是否相等*/
    @PostMapping("checkUserName")
    public Result checkUserName(String username){
        Result result = userService.checkUserName(username);
        return result;
    }
    /*
    * 注册*/
   @PostMapping("register")
    public Result register(@RequestBody User user){
        Result result = userService.register(user);
        return result;
   }
   /*
   * 修改密码*/
    @PutMapping("updatePassword")
    public Result updatePassword(@RequestHeader String token,@RequestParam("newPassword") String newPassword)
    {
        Result result=userService.updatePassword(token,newPassword);
        return result;
    }

    @PostMapping("avatar")
    public String uploadImg(MultipartFile file)throws IOException {
        return Upload.uploadImage(file);
    }

/*用户上传头像*/
   @PostMapping("avatar/insert")
    public Result avatarInsert(@RequestParam Integer id,@RequestParam("file")MultipartFile file,@RequestParam(required = false) String username,
                               @RequestParam(required = false)String name, @RequestParam(required = false) String phone, @RequestParam(required = false) String email, @RequestParam(required = false) String level)  {
       Result result = userService.avatarInsert(id,file,username,name,phone,email,level);
       return result;
   }

   /*
   * 获取用户头像*/
   @GetMapping("getAvatar")
    public Result getAvatar(@RequestParam Integer id){
        Result result=userService.getAvatar(id);
        return result;
   }
   /*
   * 分页查询*/
    @GetMapping("page")
    public Result page(@RequestParam(defaultValue = "1") Integer pageNum,
                       @RequestParam(defaultValue = "10") Integer pageSize) {
        Result result=userService.page(pageNum,pageSize);
        return result;
    }
}
